package com.tbl.gamecaro;



import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;

public class PlayGame extends Activity {
    android.widget.ImageButton img1,img2,img3,img4,img5,img6,img7,img8,img9;
    int check =1;
	boolean[] state = {false,false,false,
			           false,false,false,
			           false,false,false}; 
	int[][] xo = new int[3][3];
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        thamchieu();
        for (int i = 0; i < xo.length; i++) {
			for (int j = 0; j < xo.length; j++) {
				xo[i][j] = 2; // x:0 y:1
			}
		}
        
    }
    public void thamchieu(){
    	//TODO : 	tham chieu den cac doi tuong imagebutton trong xml
    	img1 = (android.widget.ImageButton)findViewById(R.id.imageButton1);    	
    	img2 = (android.widget.ImageButton)findViewById(R.id.imageButton2);
    	img3 = (android.widget.ImageButton)findViewById(R.id.imageButton3);
    	img4 = (android.widget.ImageButton)findViewById(R.id.imageButton4);
    	img5 = (android.widget.ImageButton)findViewById(R.id.imageButton5);
    	img6 = (android.widget.ImageButton)findViewById(R.id.imageButton6);
    	img7 = (android.widget.ImageButton)findViewById(R.id.imageButton7);
    	img8 = (android.widget.ImageButton)findViewById(R.id.imageButton8);
    	img9 = (android.widget.ImageButton)findViewById(R.id.imageButton9);
    	
    }
    public void kt(){
    	int x = 0;// 0: X 1 :O
    	int y = 0;
    	if(check>4){
    		if((xo[0][0]==0&&xo[0][1]==0&&xo[0][2]==0)||(xo[1][0]==0&&xo[1][1]==0&&xo[1][2]==0)||(xo[2][0]==0&&xo[2][1]==0&&xo[2][2]==0)
               ||(xo[0][0]==0&&xo[1][0]==0&&xo[2][0]==0)||(xo[0][1]==0&&xo[1][1]==0&&xo[2][1]==0)||(xo[0][2]==0&&xo[1][2]==0&&xo[2][2]==0)
               ||(xo[0][0]==0&&xo[1][1]==0&&xo[2][2]==0)||(xo[0][2]==0&&xo[1][1]==0&&xo[2][0]==0)
               
    				){
    			android.util.Log.i("KQ","X thang");
    			x = 0;
    			y=1;
    		}
    		else if((xo[0][0]==1&&xo[0][1]==1&&xo[0][2]==1)||(xo[1][0]==1&&xo[1][1]==1&&xo[1][2]==1)||(xo[2][0]==1&&xo[2][1]==1&&xo[2][2]==1)
    	               ||(xo[0][0]==1&&xo[1][0]==1&&xo[2][0]==1)||(xo[0][1]==1&&xo[1][1]==1&&xo[2][1]==1)||(xo[0][2]==1&&xo[1][2]==1&&xo[2][2]==1)
    	               ||(xo[0][0]==1&&xo[1][1]==1&&xo[2][2]==1)||(xo[0][2]==1&&xo[1][1]==1&&xo[2][0]==1)
    	               
    	    				){
    	    			android.util.Log.i("KQ","0 thang");
    	    			x =1;
    	    			y=1;
    	    		}
    		else if(check>9){
    			android.util.Log.i("KQ","Hue");
    			x=2;
    			y=1;
    		}
    		
    	
    	}
    	
    	if(y==1){
    		android.os.Bundle bundle = new Bundle();
        	android.content.Intent intent = new android.content.Intent(getApplicationContext(),Result.class);
    		bundle.putInt("thang", x);
    		intent.putExtras(bundle);
    		startActivity(intent);
    	}
    
    	
    }
    public void img1Click(android.view.View view){
    	if(!state[0])//
    	{
    		
	    	if((check%2)!=0){
	    		//x
	    		img1.setImageResource(R.drawable.x);
	    		
	    		
	    	}else{
	    		//o
	    		img1.setImageResource(R.drawable.o);
	    		
	    	}
	    	check++;
	    	
	    	xo[0][0] = check%2 ;
	    	
	    	state[0]=true;
	    	kt();
    	}

    }
	public void img2Click(android.view.View view){
		if(!state[1])//
    	{
			
		if((check%2)!=0){
    		//x
    		img2.setImageResource(R.drawable.x);
    		
    	}else{
    		//o
    		img2.setImageResource(R.drawable.o);
    		
    	}
    	check++;
    	state[1]=true;
    	xo[0][1] = check%2 ;
    	kt();
    	}
	}
	public void img3Click(android.view.View view){
		if(!state[2])//
    	{
			
		if((check%2)!=0){
    		//x
    		img3.setImageResource(R.drawable.x);
    		
    	}else{
    		//o
    		img3.setImageResource(R.drawable.o);
    		
    	}
    	check++;	
    	state[2]=true;
    	xo[0][2] = check%2 ;
    	kt();
    	}
	
	}
	public void img4Click(android.view.View view){
		if(!state[3])//
    	{
			
		if((check%2)!=0){
    		//x
    		img4.setImageResource(R.drawable.x);
    		
    	}else{
    		//o
    		img4.setImageResource(R.drawable.o);
    		
    	}
    	check++;
    	state[3]=true;
    	xo[1][0] = check%2 ;
    	kt();
    	}
	}
	public void img5Click(android.view.View view){
		if(!state[4])//
    	{
			
		if((check%2)!=0){
    		//x
    		img5.setImageResource(R.drawable.x);
    		
    	}else{
    		//o
    		img5.setImageResource(R.drawable.o);
    		
    	}
    	check++;
    	state[4]=true;
    	xo[1][1] = check%2 ;
    	}
	}
	public void img6Click(android.view.View view){
		if(!state[5])//
    	{
			
		if((check%2)!=0){
    		//x
    		img6.setImageResource(R.drawable.x);
    		
    	}else{
    		//o
    		img6.setImageResource(R.drawable.o);
    		
    	}
    	check++;
    	state[5]=true;
    	xo[1][2] = check%2 ;
    	kt();
    	}
	}
	public void img7Click(android.view.View view){
		if(!state[6])//
    	{
			
		if((check%2)!=0){
    		//x
    		img7.setImageResource(R.drawable.x);
    		
    	}else{
    		//o
    		img7.setImageResource(R.drawable.o);
    		
    	}
    	check++;
    	state[6]=true;
    	xo[2][0] = check%2 ;
    	kt();
    	}
	}
	public void img8Click(android.view.View view){
		if(!state[7])//
    	{
			
		if((check%2)!=0){
    		//x
    		img8.setImageResource(R.drawable.x);
    		
    	}else{
    		//o
    		img8.setImageResource(R.drawable.o);
    		
    	}
    	check++;
    	state[7]=true;
    	xo[2][1] = check%2 ;
    	kt();
    	}
	}
	public void img9Click(android.view.View view){
		if(!state[8])//
    	{
			
		if((check%2)!=0){
    		//x
    		img9.setImageResource(R.drawable.x);
    		
    	}else{
    		//o
    		img9.setImageResource(R.drawable.o);
    		
    	}
    	check++;
    	state[8]=true;
    	xo[2][2] = check%2 ;
    	kt();
    	}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		 if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	             showDialog(1);
	        }
	        return super.onKeyDown(keyCode, event);
		
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		Builder builder = new Builder(this);
		builder.setTitle("Thông báo:");
		builder.setMessage("Bạn muốn chơi tiếp ?");
		builder.setPositiveButton("Chơi Tiếp", new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();

			}
		});
		builder.setNegativeButton("Thoát hẳn", new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				moveTaskToBack(true);
			}
		});
		builder.setNeutralButton("Về Home", new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
	            android.content.Intent intent = new android.content.Intent(getApplicationContext(),Splash.class);
				startActivity(intent);
			}
		});
		
		return builder.create();
	}
    
}
